<template>
  <view class="container">
    <z-paging-swiper>
      <view slot="top">
        <u-navbar height="50" title="我的拼团" title-size="36" title-color="#333"></u-navbar>
        <view class="tabsView">
          <u-tabs-swiper
            ref="uTabs"
            :list="tabsList"
            :current="current"
            @change="tabsChange"
            :is-scroll="false"
            :show-bar="false"
            active-color="#10A28F"
            swiper-width="750"
          >
          </u-tabs-swiper>
        </view>
        <u-gap height="20" bg-color="#F0F1F2"></u-gap>
      </view>
      <swiper
        class="swiperView"
        :current="scurrent"
        @transition="transition"
        @animationfinish="animationfinish"
      >
        <swiper-item class="swiperItem" v-for="(item2, index) in tabsList" :key="index">
          <z-paging
            ref="paging"
            v-model="itemList[index]"
            @query="queryList"
            :fixed="true"
            :auto="false"
            :empty-view-img="`${getImageUrl('empty.png')} `"
            empty-view-text="暂无订单数据~"
            :empty-view-img-style="imgStyle"
          >
            <view class="goodsItemView">
              <view class="itemView" v-for="(item, index1) in itemList[index]" :key="index1">
                <view
                  @click="onOrderDetail(item)"
                  class="order-top dis-flex flex-y-center flex-y-between"
                >
                  <view
                    class="dis-flex flex-y-center u-line-1"
                    style="flex: 1; margin-right: 48rpx"
                  >
                    <u-avatar size="50"></u-avatar>
                    <view class="shop-name">{{ item.shop ? item.shop.name : '店铺名称' }}</view>
                  </view>
                  <view class="order-stat" v-if="item.status == 0">
                    拼团中，还差{{ item.userSize - item.userCount }}人拼团成功
                  </view>
                  <view v-else-if="item.status == 1">
                    <u-icon name="/static/users/ywc.png" size="60"></u-icon>
                  </view>
                  <view v-else>
                    <u-icon name="/static/users/ysb.png" size="60"></u-icon>
                  </view>
                </view>

                <view class="order-goods" @click="onOrderDetail(item)">
                  <u-image
                    class=""
                    :name="item.picUrl"
                    width="220rpx"
                    height="220rpx"
                    border-radius="10"
                  ></u-image>
                  <view class="orde-riget" style="flex: 1">
                    <view class="goods-title">
                      {{ item.spuName }}
                    </view>
                    <view class="goods-speci"> </view>
                    <view
                      class="dis-flex flex-y-center flex-y-between"
                      style="margin-top: 10px; width: 100%"
                    >
                      <view class="goods-price"
                        >拼团价¥
                        <text class="bold">{{
                          (item.combinationPrice * 0.01).toFixed(2)
                        }}</text></view
                      >
                      <text class="goods-num">x{{ item.count }}</text>
                    </view>
                  </view>
                </view>

                <!-- 	共1样商品 -->
                <view class="price-total">
                  共1样商品 实付：¥{{ (item.count * item.combinationPrice * 0.01).toFixed(2) }}
                </view>
                <view class="btn-group" v-if="item.status == 0">
                  <text style="flex: 1"
                    >{{ item.userSize }}人团，距离成功还剩{{ countdown(item.expireTime) }}</text
                  >
                  <view class="solid-btn btn" @click="show = true"> 邀请好友 </view>
                </view>
                <view class="btn-group" v-else @click="onOrderDetail(item)">
                  <text style="flex: 1"
                    >{{ item.userSize }}人团{{ item.status == 1 ? '成功' : '失效' }}</text
                  >
                  <view class="hollow-btn btn"> 查看详情 </view>
                </view>
              </view>
            </view>
          </z-paging>
        </swiper-item>
      </swiper>
    </z-paging-swiper>
    <u-popup v-model="show" height="auto" border-radius="15" mode="bottom">
      <view class="share-box">
        <view class="" style="font-size: 30rpx; text-align: center; padding: 50rpx; color: #666666">
          还差一人，去邀请你好友吧
        </view>
        <view class="group dis-flex flex-y-around">
          <view class="group-item">
            <u-icon name="/static/wxhy.png" size="96"></u-icon>
            <view>微信好友</view>
          </view>
          <view class="group-item">
            <u-icon name="/static/pyq.png" size="96"></u-icon>
            <view>朋友圈</view>
          </view>
        </view>
        <u-gap height="20" bg-color="#F0F1F2" />
        <view
          class=""
          @click="show = false"
          style="font-size: 30rpx; height: 100rpx; line-height: 100rpx; text-align: center"
        >
          取消
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imgStyle: {
        width: '12rem'
      },
      show: false,
      current: 0,
      scurrent: 0,
      tabsList: [
        {
          name: '全部',
          status: ''
        },
        {
          name: '拼团中',
          status: 0
        },
        {
          name: '拼团成功',
          status: 1
        },
        {
          name: '已失效',
          status: 2
        }
      ],
      itemList: [[], [], [], []]
    }
  },
  onLoad() {
    this.queryList()
  },
  computed: {
    countdown: function () {
      return (item) => {
        let now = new Date().getTime()
        if (item > now) {
          let difference = item - now
          console.log(',,', difference)
          if (difference > 24 * 3600000) {
            return Math.floor(difference / (24 * 3600000)) + '天'
          } else if (difference > 3600000) {
            return Math.floor(difference / 3600000, '小时') + '小时'
          } else if (difference > 6000) {
            return Math.floor(difference / 6000) + '分'
          }
          return '即将超时'
        }
      }
    }
  },
  methods: {
    tabsChange(index) {
      this.scurrent = index
    },
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    animationfinish(e) {
      this.current = e.detail.current
      this.scurrent = e.detail.current
      this.$refs.uTabs.setFinishCurrent(this.scurrent)
      if (this.itemList[this.scurrent].length == 0) {
        this.queryList()
      }
    },

    queryList(page = 1, pageSize = 10) {
      this.$u
        .get('/app-api/promotion/combination-record/page', {
          pageNo: page,
          pageSize,
          status: this.tabsList[this.scurrent].status
        })
        .then((res) => {
          this.$refs.paging[this.scurrent].complete(res.data.list)
        })
    },

    onOrderDetail(item) {
      this.$utils.route({
        url: '/goods/group-order-details/group-order-details',
        params: {
          id: item.id,
          orderId: item.orderId
        }
      })
    },
    onShare() {}
  }
}
</script>

<style lang="scss">
@import url('group-order.css');

page {
  background: #eeeeef;
}
.share-box {
  .group {
    margin: 20rpx;
    .group-item {
      text-align: center;
      font-size: 24rpx;
      color: #333333;
      line-height: 44rpx;
    }
  }
}
</style>
